package net.vkits.controller;

import net.vkits.admin.application.PostApplicationService;
import net.vkits.admin.domain.model.FakeUser;
import net.vkits.admin.domain.model.Post;
import net.vkits.admin.domain.repository.FakeUserRepository;
import net.vkits.common.Utils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by ethan-liu on 2017/2/3.
 */
@Controller
@RequestMapping("/posts")
public class PostsController {
    private static final Logger logger = LoggerFactory.getLogger(PostsController.class);

    @Resource
    private PostApplicationService postApplicationService;

    @Resource
    private FakeUserRepository fakeUserRepository;

    @RequiresPermissions("system:sys_role:list")
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView posts() {
        String username = Utils.getUsername(SecurityUtils.getSubject()).orElse("Null");
        List<FakeUser> fakeUsers = fakeUserRepository.findAll();
        ModelMap modelMap = new ModelMap();
        modelMap.put("treeview", "content");
        modelMap.put("sidebar", "posts");
        modelMap.put("currentUser",username);
        modelMap.put("fakeUsers",fakeUsers);
        return new ModelAndView("/posts", modelMap);
    }

    @RequiresPermissions("system:sys_role:list")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Page<Post> getPosts(@PageableDefault(value = 10, sort = {"id"}, direction = Sort.Direction.DESC)Pageable pageable) {
        Page<Post> posts = postApplicationService.getPosts(pageable);
        logger.info("size is: {}", posts.getTotalElements());
        return posts;
    }

    @RequiresPermissions("system:sys_role:add")
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public String addPost(@RequestBody Post post) {
        logger.info("add Post: {}", post);
        FakeUser fakeUser = fakeUserRepository.findOne(Long.parseLong(post.getNickname()));
        postApplicationService.addPost(fakeUser.getNickname(),
                "13811480360",
                fakeUser.getAvatarUrl(),
                post.getContent(),
                post.getReadNum(),
                post.isVisibility(),
                post.getLikeNum());
        return "done";
    }

    @RequiresPermissions("orders:modify")
    @RequestMapping(value ="/{id}", method = RequestMethod.GET)
    public ModelAndView post(@PathVariable Long id) {
        logger.info("id: {}", id);
        String username = Utils.getUsername(SecurityUtils.getSubject()).orElse("Null");
        Post post = postApplicationService.getPost(id);
        List<FakeUser> fakeUsers = fakeUserRepository.findAll();
        ModelMap modelMap = new ModelMap();
        modelMap.put("post", post);
        modelMap.put("treeview", "content");
        modelMap.put("sidebar", "posts");
        modelMap.put("currentUser", username);
        modelMap.put("fakeUsers", fakeUsers);
        return new ModelAndView("/post", modelMap);
    }

    @RequiresPermissions("orders:modify")
    @RequestMapping(value ="/update", method = RequestMethod.POST)
    public String postUpdate(@RequestParam(value = "postId", required = true) Long postId,
                             @RequestParam(value = "readNum", defaultValue = "0") int readNum,
                             @RequestParam(value = "likeNum", defaultValue = "0") int likeNum,
                             @RequestParam(value = "visibility", required = true) boolean visibility,
                                 @RequestParam(value = "content", defaultValue = "") String content) {
        postApplicationService.updatePost(postId, readNum, likeNum, visibility, content);
        return "redirect:/posts";
    }

    @RequiresPermissions("system:sys_role:del")
    @RequestMapping(value ="/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public String delPost(@PathVariable Long id) {
        logger.info("del Post: {}", id);
        postApplicationService.delPost(id);
        return "done";
    }
}
